package types

import (
	"github.com/looplab/fsm"
)

const (
	Start   = "start"
	End     = "end"
	Confirm = "confirm"
	//CaloModelId 校准仪 物模型ID.
	CaloModelId = "m_calo"

	//ZeroCalibrate 零气校准.
	ZeroCalibrate = "ZeroCalibrate"
	//CancelZeroCalibrate 取消跨度校准.
	CancelZeroCalibrate = "CancelSpanCalibrate"
	//ConfirmZeroCalibrate 确认跨度校准.
	ConfirmZeroCalibrate = "ConfirmSpanCalibrate"

	//SpanCalibrate 跨度校准.
	SpanCalibrate = "SpanCalibrate"
	//CancelSpanCalibrate 取消跨度校准.
	CancelSpanCalibrate = "CancelSpanCalibrate"
	//ConfirmSpanCalibrate 确认跨度校准.
	ConfirmSpanCalibrate = "ConfirmSpanCalibrate"
	//CaliFlow 校准仪流量.
	CaliFlow = "4000"

	//IndicationError 示值误差.
	IndicationError = "IndicationError"
	//LinearityError 线性误差.
	LinearityError = "LinearityError"
	//Precision 精密度.
	Precision = "Precision"
	//ZeroPointNoise 零点噪声.
	ZeroPointNoise = "ZeroPointNoise"
	//RangeNoise 量程噪声
	RangeNoise = "RangeNoise"
	//ZeroDriftAndSpanDrift 24H零点漂移和量程漂移
	ZeroDriftAndSpanDrift = "ZeroDriftAndSpanDrift"
	// ZeroDriftAndSpanDriftHeaven 零点漂移和量程漂移需要运行的参数
	ZeroDriftAndSpanDriftHeaven = "7"
)

var (
	//CaliMapEvent 任务事件
	//配置任务的执行流程
	CaliMapEvent = map[string]fsm.Events{
		IndicationError:       IndicationErrorEvent,
		LinearityError:        LinearityErrorEvent,
		Precision:             PrecisionEvent,
		ZeroPointNoise:        ZeroPointNoiseEvent,
		RangeNoise:            RangeNoiseEvent,
		ZeroDriftAndSpanDrift: ZeroDriftAndSpanDriftEvent,
	}

	//Status 气体状态.
	Status = "status"
	// CalibratorZero 校准仪 零气校准。
	CalibratorZero = map[string]string{
		Start: "calzero",
		End:   "calzend",
	}
	//CalibratorSpan 校准仪 跨度校准.
	CalibratorSpan = map[string]string{
		Start: "calspan",
		End:   "calsend",
	}
	//GasCalibratorZero 零气校准命令.
	GasCalibratorZero = map[string]map[string]string{
		"m_so2": {
			Start:   "szeros",
			End:     "szeroe",
			Confirm: "szeroq",
		},
		"m_co": {
			Start:   "czeros",
			End:     "czeroe",
			Confirm: "czeroq",
		},
		"m_nox": {
			Start:   "nzeros",
			End:     "nzeroe",
			Confirm: "nzeroq",
		},
		"m_o3": {
			Start:   "ozeros",
			End:     "ozeroe",
			Confirm: "ozeroq",
		},
	}

	//GasCalibratorSpan 跨度校准命令.
	GasCalibratorSpan = map[string]map[string]string{
		"m_so2": {
			Start:   "sspans",
			End:     "sspane",
			Confirm: "sspanq",
		},
		"m_co": {
			Start:   "cspans",
			End:     "cspane",
			Confirm: "cspanq",
		},
		"m_nox": {
			Start:   "nspans",
			End:     "nspane",
			Confirm: "nspanq",
		},
		"m_o3": {
			Start:   "ospans",
			End:     "ospane",
			Confirm: "ospanq",
		},
	}

	//CalibratorType 校准类型.
	CalibratorType = map[string]string{
		"m_o3":  "O",
		"m_so2": "S",
		"m_nox": "N",
		"m_co":  "C",
	}
	//Range 量程.
	Range = map[string]float64{
		"m_co":  50,
		"m_o3":  500,
		"m_so2": 500,
		"m_nox": 500,
	}

	//CaliFlowDefault 默认校准流量.
	CaliFlowDefault = map[string]int{
		"m_co":  4000,
		"m_o3":  4000,
		"m_so2": 4000,
		"m_nox": 4000,
	}
	//TaskCaliType 校准任务类型.
	TaskCaliType = []string{
		IndicationError,
		LinearityError,
		Precision,
	}
	//IndicationErrorRange 通入浓度约为50%量程的标准气体.
	IndicationErrorRange = 0.5
	//LinearityErrorRang 线性误差对应的百分比.
	LinearityErrorRang = map[string]float64{
		"0.2": 0.2,
		"0.4": 0.4,
		"0.6": 0.6,
		"0.8": 0.8,
	}
	//PrecisionRang 精密度百分比.
	PrecisionRang = map[string]float64{
		"0.2": 0.2,
		"0.8": 0.8,
	}
	//IndicationErrorEvent 示值误差 执行流程.
	IndicationErrorEvent = fsm.Events{
		{Name: "1", Src: []string{"1"}, Dst: "2"},
		{Name: "2", Src: []string{"2"}, Dst: "3"},
		{Name: "3", Src: []string{"3"}, Dst: "4"},
		{Name: "4", Src: []string{"4"}, Dst: "END"},
	}

	LinearityErrorEvent = fsm.Events{
		{Name: "1", Src: []string{"1"}, Dst: "2"},
		{Name: "2", Src: []string{"2"}, Dst: "3"},
		{Name: "3", Src: []string{"3"}, Dst: "END"},
	}
	PrecisionEvent = fsm.Events{
		{Name: "1", Src: []string{"1"}, Dst: "2"},
		{Name: "2", Src: []string{"2"}, Dst: "3"},
		{Name: "3", Src: []string{"3"}, Dst: "4"},
		{Name: "4", Src: []string{"4"}, Dst: "5"},
		{Name: "5", Src: []string{"5"}, Dst: "6"},
		{Name: "6", Src: []string{"6"}, Dst: "END"},
	}
	ZeroPointNoiseEvent = fsm.Events{
		{Name: "1", Src: []string{"1"}, Dst: "END"},
	}
	RangeNoiseEvent = fsm.Events{
		{Name: "1", Src: []string{"1"}, Dst: "END"},
	}
	ZeroDriftAndSpanDriftEvent = fsm.Events{
		{Name: "1", Src: []string{"1"}, Dst: "END"},
		//{Name: "2", Src: []string{"2"}, Dst: "3"},
		//{Name: "3", Src: []string{"3"}, Dst: "4"},
		//{Name: "4", Src: []string{"4"}, Dst: "5"},
		//{Name: "5", Src: []string{"5"}, Dst: "6"},
		//{Name: "6", Src: []string{"6"}, Dst: "7"},
		//{Name: "7", Src: []string{"7"}, Dst: "END"},
	}
	//CaliTaskPrams 任务参数.
	CaliTaskPrams = map[string]map[string]GroupTask{
		IndicationError: {
			"1": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.5, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 2,
			},
			"2": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.5, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 2,
			},
			"3": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.5, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 2,
			},
			"4": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.5, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 2,
			},
		},
		LinearityError: {
			"1": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.4, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.6, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 8,
			},
			"2": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.4, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.6, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 8,
			},
			"3": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.4, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.6, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 8,
			},
		},
		Precision: {
			"1": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 4,
			},
			"2": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 4,
			},
			"3": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 4,
			},
			"4": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 4,
			},
			"5": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 4,
			},
			"6": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
					{GasType: 0, Number: 0, Ext: 5, Unit: "PPB", ValueTime: []int{}},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{26, 27, 28}},
				},
				TaskNum: 4,
			},
		},
		ZeroPointNoise: {
			"1": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 120, Unit: "PPB", ValueTime: []int{}, EveryTime: 2},
				},
				TaskNum: 1,
			},
		},
		RangeNoise: {
			"1": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 1, Number: 0.8, Ext: 120, Unit: "PPB", ValueTime: []int{}, EveryTime: 2},
				},
				TaskNum: 1,
			},
		},
		ZeroDriftAndSpanDrift: {
			"1": GroupTask{
				Subtasks: []CaliParams{
					{GasType: 0, Number: 0, Ext: 30, Unit: "PPB", ValueTime: []int{28}, EveryTime: 0},
					{GasType: 1, Number: 0.2, Ext: 30, Unit: "PPB", ValueTime: []int{28}, EveryTime: 0},
					{GasType: 1, Number: 0.8, Ext: 30, Unit: "PPB", ValueTime: []int{28}, EveryTime: 0},
				},
				TaskNum: 3,
			},
		},
	}
)

//GetTaskTotalNum 获取任务总数量.
func GetTaskTotalNum(taskType string) int {
	if values, ok := CaliTaskPrams[taskType]; ok {
		sum := 0
		for i := range values {
			sum += values[i].TaskNum
		}
		return sum
	} else {
		return 0
	}
}
func GetUnit(modelId string) string {
	if modelId == "m_co" {
		return "PPM"
	} else {
		return "PPB"
	}
}

func IsZero(gasType string) int {
	if gasType == ZeroCalibrate {
		return 0
	}
	return 1
}

//零气校准，和跨度校准，计划任务可以传溶度.

func IsCali(gasType string) bool {
	if gasType == ZeroCalibrate || gasType == SpanCalibrate {
		return true
	}
	return false
}
